import pygame
import random

pygame.init()
screen = pygame.display.set_mode((500,  700))
is_loop = True
points = []
clock = pygame.time.Clock()
font = pygame.font.SysFont('Arial', 30)
score = 0
rect = pygame.rect.Rect(200, 600,100, 30)
index = 150
while is_loop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_loop = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                rect.x -= 10
                if rect.x <0:
                    rect.x = 10
            elif event.key == pygame.K_RIGHT:
                rect.x += 10
                if rect.x+rect.w >500:
                    rect.x = 500-rect.w-10
    screen.fill((0,0,0))
    i = 0
    while i < len(points):
        point = points[i]
        pygame.draw.circle(screen, (255,0,0),point, 20)
        point[1]+=1
        if point[1]>580 and point[1]< 600:
            if point[0]>rect.x and point[0]< rect.x+rect.w:
                points.pop(i)
                score+=1
                continue
        i+=1


    score_image = font.render(f"Score:{score}", False, (255,0,0))
    screen.blit(score_image, (0, 0))
    pygame.draw.rect(screen, (0, 255, 0),rect)
    if index == 150:
        index =0
        points.append([random.randint(20, 480), 0])
    else:
        index+=1
    clock.tick(80)
    pygame.display.update()
pygame.quit()